home *** CD-ROM | disk | FTP | other *** search
- Path: news.zocalo.net!news
- From: Paul Hsieh <qed@xenon.chromatic.com>
- Newsgroups: comp.lang.c,comp.graphics.algorithms,rec.games.programmer
- Subject: Re: Speed question here...
- Date: Mon, 19 Feb 1996 21:15:43 -0800
- Organization: Zocalo Engineering - Berkeley, California, USA
- Message-ID: <312958FF.6F1C@xenon.chromatic.com>
- References: <4ftluh$1gkv@hearst.cac.psu.edu> <4g9rbg$3cl@ooze.val.net>
- NNTP-Posting-Host: paulh.chromatic.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Andrea Griffini wrote:
- > koscho@wjk130.rh.psu.edu (William Koscho) wrote:
- > >I was curious as to how fast something like the
- > >following would execute:
- >
- > > int x;
- > > node *ptr; ptr in linked list
- >
- > > for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
- > > for ( x = 0; x < max; x++ ) {
- > > array[x] = array_other[x];
- > > }
- > > }
-
-
- Uh ... unless array and ptr are somehow linked in a way that is not
- obvious here, this code is awefully silly. Chances are you meant
- something more along the lines of:
-
- for( ptr=first_node; ptr!=NULL; ptr=ptr->next ) {
- for( x=0; x<max; x++ ) {
- ptr->array[x] = array_init[x];
- }
- }
-
- N'est-ce pas? In this case, you are overlooking obvious potential
- for platform specific improvements. Most CPUs have some sort of
- memory copy that's faster than element by element assignment. With-
- out resorting to assembly language, this is available in the form
- of "memcpy". So more than likely this is a better solution:
-
- int arraylen = max*sizeof(ptr->array[0]);
-
- for( ptr=first_node; ptr!=NULL; ptr=ptr->next ) {
- memcpy( ptr->array, array_init, arraylen);
- }
-
- Moral of the story: Know your C library.
-
- --
- Paul Hsieh
- qed@xenon.chromatic.com
-
- What I say and what my company says are not always the same thing
-